/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.filter;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.entity.CamposRelatorio;
import static br.jm.financeiro.filter.CamposRelatorioConverter.camposDB;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author joao
 */
public class CamposRelatorioConverter implements Converter {

    public static List<CamposRelatorio> camposDB = new ArrayList<>();

    public CamposRelatorioConverter() throws EaoException {
        camposDB.add(new CamposRelatorio(9, "nom_fantasia", "Cliente"));
        camposDB.add(new CamposRelatorio(3, "dsc_empreendimento", "Empreendimento"));
        camposDB.add(new CamposRelatorio(11, "num_processo", "Processo"));
        camposDB.add(new CamposRelatorio(5, "dsc_lote", "Assunto"));
        camposDB.add(new CamposRelatorio(4, "dsc_id_lote", "Lote"));
        camposDB.add(new CamposRelatorio(6, "dsc_orgao", "Unidade"));
        camposDB.add(new CamposRelatorio(7, "loc_dat_desde", "Desde"));
        camposDB.add(new CamposRelatorio(12, "sit_descricao", "Situação"));
        camposDB.add(new CamposRelatorio(8, "loc_funciionario", "Funcionário"));
        camposDB.add(new CamposRelatorio(10, "nom_tecnico", "Nome do Técnico"));
        camposDB.add(new CamposRelatorio(2, "dpt_responsavel", "Depto.Responsavel"));
        camposDB.add(new CamposRelatorio(1, "dat_update", "Data Atualização"));
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.trim().equals("")) {
            return null;
        } else {
            try {
                int number = Integer.parseInt(value);

                for (CamposRelatorio p : camposDB) {
                    if (p.getId() == number) {
                        return p;
                    }
                }

            } catch (NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String cReturn = "";
        try {
            if (value == null || value.equals("")) {
                return "";
            } else {
                cReturn = value.toString();
            }
        } catch (Exception e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
        }
        return cReturn;
    }

}
